﻿using UnityEngine;
using System.Collections;

public class BossMovement : MonoBehaviour
{
	[SerializeField] Transform target;
	NavMeshAgent navAgent;                      //怪物的Navigation组件
	Animator anima;

	[SerializeField] float moveSpeed = 2f;                     //移速

	void Start()
	{
		navAgent = GetComponent<NavMeshAgent>();
		anima = GetComponent<Animator> ();
	}

	void Update ()
	{
		if (navAgent.enabled && target != null) {
			navAgent.speed = moveSpeed;
			anima.enabled = true;
			Vector3 targetPos = target.position;
			//以下一行是为了避免靠得太近出问题
			targetPos.y = transform.position.y;
			navAgent.SetDestination (targetPos);
		}
	}

	/// <summary>
	/// 设置boss的移动目标，boss会自动寻路
	/// </summary>
	/// <param name="targetPos">Target position.</param>
	public void setDestination(Vector3 targetPos){
		navAgent.enabled = true;
		anima.enabled = true;
		transform.LookAt(targetPos);
		navAgent.SetDestination(targetPos);//导航至player所在的位置
	}
	/// <summary>
	/// 设置boss的移动目标，boss会自动寻路
	/// </summary>
	/// <param name="targetTransform">Target transform.</param>
	public void setDestination(Transform targetTransform){
		navAgent.enabled = true;
		anima.enabled = true;
		target = targetTransform;
		transform.LookAt(targetTransform.position);
		navAgent.SetDestination(targetTransform.position);//导航至player所在的位置
	}
	/// <summary>
	/// 设置boss的移动目标，boss会自动寻路
	/// </summary>
	/// <param name="targetPos">Target position.</param>
	/// <param name="setSpeed">Set speed.</param>
	public void setDestination(Vector3 targetPos, float setSpeed){
		navAgent.enabled = true;
		anima.enabled = true;
		navAgent.speed = setSpeed;
		transform.LookAt(targetPos);
		navAgent.SetDestination(targetPos);//导航至player所在的位置
	}
	/// <summary>
	/// 设置boss的移动目标，boss会自动寻路
	/// </summary>
	/// <param name="targetTransform">Target transform.</param>
	/// <param name="setSpeed">Set speed.</param>
	public void setDestination(Transform targetTransform, float setSpeed){
		navAgent.enabled = true;
		anima.enabled = true;
		target = targetTransform;
		navAgent.speed = setSpeed;
		transform.LookAt(targetTransform.position);
		navAgent.SetDestination(targetTransform.position);//导航至player所在的位置
	}
	/// <summary>
	/// 让boss停止自动寻路
	/// </summary>
	public void stopBoss(){
		target = null;
		navAgent.enabled = false;
	}
}